"""演示一个线程锁如何实现程序的顺序执行"""
from threading import Thread,Lock
import random,time
lock = Lock()
ls=[i for i in range(1,9)]
random.shuffle(ls)
print("生成的列表是:",list(map(lambda x:str(x),ls)))

def worker(name):
    lock.acquire()
    print(name)
    time.sleep(random.randint(1, 2))
    lock.release()


threads=[]
for item in ls:
    t = Thread(target=worker, args=(item,))
    t.start()
    threads.append(t)
for thread in threads:
    thread.join()

